package descriptors;

import modulIOContainer.Patch;
import ij.ImagePlus;
import ij.process.ColorProcessor;

public class PatchDescriptor implements IDescriptor{
	
	private ImagePlus image;
	
	private ColorProcessor cp;	
	private byte[] H,S,B;
	
	public Patch getPatch(float x, float y, int size) {
		Patch p= new Patch();
		
		p.attributes = new double[size * size * 3];
		
		int xStart=(int) Math.ceil(x*image.getWidth()-size/2.0f);
		int yStart=(int) Math.ceil(y*image.getHeight()-size/2.0f);
		
		int i = 0;
		for (int yi = yStart; yi < yStart + size; yi++)
			for (int xi = xStart; xi < xStart + size; xi++) {
				p.attributes[i] = H[xi + yi * cp.getWidth()];
				p.attributes[i + size * size] = S[xi + yi
						* cp.getWidth()];
				p.attributes[i + size * size * 2] = B[xi + yi
						* cp.getWidth()];
				i++;
			}
		return p;
	}
	
	public void setImage(ImagePlus image) {
		this.image=image;
		
		//extract pixels in HSV-Format
		cp = new ColorProcessor(image.getImage());
		
		H = new byte[cp.getWidth()*cp.getHeight()];
		S = new byte[cp.getWidth()*cp.getHeight()];
		B = new byte[cp.getWidth()*cp.getHeight()];
		cp.getHSB(H, S, B);
		
	}

}
